//
//  SliderView.m
//  NewMusic
//
//  Created by chuxinyu123 on 15/11/12.
//  Copyright © 2015年 Going. All rights reserved.
//

#import "SliderView.h"

@interface SliderView ()

@property (nonatomic, assign) NSInteger num;
@end
@implementation SliderView

- (void)dealloc
{
    [_scroller release];
    [_imageViewLine release];
    [super dealloc];
}

- (UIImageView *)imageViewLine
{
    if (nil == _imageViewLine) {
        self.imageViewLine = [[UIImageView alloc] initWithFrame:CGRectMake(15, self.frame.size.height - 2, 50, 2)];
        self.imageViewLine.backgroundColor = [UIColor colorWithRed:30 / 255.0 green:177 / 255.0 blue:78 / 255.0 alpha:1.0];
        [_imageViewLine release];
    }
    return _imageViewLine;
}

//初始化
- (instancetype)initWithFrame:(CGRect)frame andNSArray:(NSArray *)sliderArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.num = sliderArray.count;
        [self creatSubView:sliderArray];
    }
    return self;
}


- (void)creatSubView:(NSArray *)sliderArray
{
    UIView *viewLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 1, [UIScreen mainScreen].bounds.size.width, 1)];
    viewLine.backgroundColor = [UIColor colorWithRed:100 / 255.0 green:100 / 255.0 blue:100 / 255.0 alpha:1.0];
    [self addSubview:viewLine];
    [viewLine release];
    
    self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, self.frame.size.height)];
    [self addSubview:_scroller];
    
    for (NSInteger i = 0; i < self.num; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10 + 90 * i, 4, 60, self.frame.size.height - 6 * 2);
        [button setTitle:[sliderArray objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:[UIColor colorWithRed:30 / 255.0 green:177 / 255.0 blue:78 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.scroller addSubview:button];
    }
    [self.scroller addSubview:self.imageViewLine];
    [_scroller release];
}


- (void)buttonAction:(UIButton *)button
{
    CGRect rect = button.frame;
    [UIView animateWithDuration:0.4f animations:^{
        self.imageViewLine.frame = CGRectMake(rect.origin.x + 5, self.frame.size.height - 2, 50, 2);
    }];
    
    [self.delegate changeCellWithButton:button.tag - 1000];
    
    for (NSInteger i = 0; i < 4; i++) {
        if (button.tag - 1000 == i) {
            [(UIButton *)[self.scroller viewWithTag:i + 1000] setTitleColor:[UIColor colorWithRed:30 / 255.0 green:177 / 255.0 blue:78 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }else{
            [(UIButton *)[self.scroller viewWithTag:i + 1000] setTitleColor:[UIColor colorWithRed:180 / 255.0 green:180 / 255.0 blue:180 / 255.0 alpha:1.0] forState:UIControlStateNormal];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
